﻿using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WorkOne.Domain;
using WorkOne.Domain.Model;
using WorkOne.IService;

namespace WorkOne.Webapi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IUserService seuser;
        private readonly GoodsDbContext db;

        public UserController(IUserService seuser, GoodsDbContext db)
        {
            this.seuser = seuser;
            this.db = db;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpGet("UserLogin")]
        public IActionResult UserLogin(string username, string password)
        {
            User res = seuser.Userlogin(username, password);
            if (res == null)
            {
                return Ok(-1);
            }
            Random random = new Random();
            var token = random.Next(1, 9999999);
            Token token1 = new Token { Tid = 0, CreateTime = DateTime.Now, TokenCode = token.ToString(), Uid = res.Uid, CreatePeople = "1", TableCreateTime = DateTime.Now, UpdatePeople = "1", UpdateTime = DateTime.Now };
            db.tokens.Add(token1);
            db.SaveChanges();
            return Ok(new
            {
                User = res,
                Token = token
            });
        }

        [HttpGet("Md5")]
        public IActionResult actionResult()
        {
            string a = "123456";
            return Ok(Md5Helper.DESEncrypt(a, "secret789"));
        }

        [HttpGet("Md52")]
        public IActionResult actionResult2(string id)
        {
            string a = "123456";
            return Ok(Md5Helper.DESDecrypt(id, "secret789"));
        }
    }
}